package ar.com.ctrln.persistence.dao.liquidacion;

import java.util.List;

import org.hibernate.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import ar.com.ctrln.persistence.dao.AbstractHibernateDao;
import ar.com.ctrln.persistence.model.liquidacion.Componente;
import ar.com.ctrln.persistence.model.liquidacion.DetalleLiquidacion;

@Repository("detalleLiquidacionDao")
public class HibernateDetalleLiquidacionDao extends
		AbstractHibernateDao<DetalleLiquidacion> implements
		DetalleLiquidacionDao {

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<Componente> listarComponentes(long idDetalle) {
		Query q = getSession()
				.createQuery(
						"Select d.componentes from DetalleLiquidacion as d where d.id = ?");
		q.setParameter(0, idDetalle);

		return q.list();
	}

	@Override
	@Transactional(readOnly = true)
	public DetalleLiquidacion get(int mes, int anio, long idLiquidacion,
			long idCliente) {
		Query q = getSession()
				.createQuery(
						"from DetalleLiquidacion as d where d.mes = ? and d.anio = ? and d.liquidacion.id = ? and cliente.id = ?");
		q.setParameter(0, mes);
		q.setParameter(1, anio);
		q.setParameter(2, idLiquidacion);
		q.setParameter(3, idCliente);
		return (DetalleLiquidacion) q.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<DetalleLiquidacion> listarDetalles(long idLiquidacion) {
		Query q = getSession().createQuery(
				"Select l.detalles from Liquidacion as l where l.id = ?");
		q.setParameter(0, idLiquidacion);

		return q.list();
	}

}
